<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入全表Excel</Button>
        </form>
        <input v-model="id" placeholder="id" ></input>
        <input v-model="gameId" placeholder="游戏id,不填为所有游戏" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <!--<Upload action="//jsonplaceholder.typicode.com/posts/" ref="upload" :on-success="onUploadSuccess">
          <Button type="ghost" icon="ios-cloud-upload-outline">导入Excel</Button>
        </Upload>-->
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出全表EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑betLevel"
        v-model="editBetLevelModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="id">
            <Input v-model="formEdit.id" disabled ></Input>
        </Form-item>
        <Form-item label="游戏Id">
            <Input v-model="formEdit.gameId" disabled ></Input>
        </Form-item>
        <Form-item label="type">
            <Input v-model="formEdit.type"></Input>
        </Form-item>
        <Form-item label="money">
            <Input v-model="formEdit.money"></Input>
        </Form-item>
        <Form-item label="bounsMin">
            <Input v-model="formEdit.bounsMin"></Input>
        </Form-item>
        <Form-item label="bounsMax">
            <Input v-model="formEdit.bounsMax"></Input>
        </Form-item>
        <Form-item label="isJackpot">
            <Input v-model="formEdit.isJackpot"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getBetLevels, getAllBetLevels, modifyBetLevel, importBetLevel} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      id: '',
      gameId: '',
      filename: 'bet_level',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      editBetLevelModal: false,
      results: '',
      formEdit: {
        id: '',
        gameId: '',
        type: '',
        money: '',
        bounsMin: '',
        bounsMax: '',
        isJackpot: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tgameId = 0
      if (this.gameId !== '') {
        tgameId = parseInt(this.gameId)
      }

      let tId = 0
      if (this.id !== '') {
        tId = parseInt(this.id)
      }

      console.log('this.tId:', tId, ',tgameId:', tgameId)
      getBetLevels(tId, tgameId, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let BetLevelVO = getVO('BetLevelVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BetLevelVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          vo.money = vo.money.toNumber()
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditBetLevel (index) {
      let data = this.tableData[index]
      this.formEdit.id = data.id
      this.formEdit.gameId = data.gameId
      this.formEdit.type = data.type
      this.formEdit.money = data.money
      this.formEdit.bounsMin = data.bounsMin
      this.formEdit.bounsMax = data.bounsMax
      this.formEdit.isJackpot = data.isJackpot

      this.editBetLevelModal = true
    },
    onSubmit () {
      if (this.formEdit.id === '' || this.formEdit.gameId === '' || this.formEdit.type === '' || this.formEdit.money === '' || this.formEdit.bounsMin === '' || this.formEdit.bounsMax === '' || this.formEdit.isJackpot === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let BetLevelVO = getVO('BetLevelVO')
      var betLevelVO = new BetLevelVO()
      betLevelVO.id = parseInt(this.formEdit.id)
      betLevelVO.gameId = Number(this.formEdit.gameId)
      betLevelVO.type = parseInt(this.formEdit.type)
      betLevelVO.money = Number(this.formEdit.money)
      betLevelVO.bounsMin = parseInt(this.formEdit.bounsMin)
      betLevelVO.bounsMax = parseInt(this.formEdit.bounsMax)
      betLevelVO.isJackpot = parseInt(this.formEdit.isJackpot)

      modifyBetLevel(betLevelVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.id === this.formEdit.id && data.gameId === this.formEdit.gameId) {
              this.tableData[i].type = this.formEdit.type
              this.tableData[i].money = this.formEdit.money
              this.tableData[i].bounsMin = this.formEdit.bounsMin
              this.tableData[i].bounsMax = this.formEdit.bounsMax
              this.tableData[i].isJackpot = this.formEdit.isJackpot
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importBetLevel(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllBetLevels().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let BetLevelVO = getVO('BetLevelVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BetLevelVO.decode(pv.data[i])
          vo.gameId = vo.gameId.toNumber()
          vo.money = vo.money.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'id',
          key: 'id',
          align: 'center'
        },
        {
          title: '游戏Id',
          key: 'gameId',
          align: 'center'
        },
        {
          title: 'type',
          key: 'type',
          align: 'center'
        },
        {
          title: 'money',
          key: 'money',
          align: 'center'
        },
        {
          title: 'bounsMin',
          key: 'bounsMin',
          align: 'center'
        },
        {
          title: 'bounsMax',
          key: 'bounsMax',
          align: 'center'
        },
        {
          title: 'isJackpot',
          key: 'isJackpot',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditBetLevel(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
